﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Partial Class propertyType
    Inherits System.Web.UI.Page

#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 10
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
      LoadPropertyType()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        LoadPropertyType()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
       LoadPropertyType()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        LoadPropertyType()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
       LoadPropertyType()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
       LoadPropertyType()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
        LoadPropertyType()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
       LoadPropertyType()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        LoadPropertyType()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
           LoadPropertyType()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        LoadPropertyType()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
            LoadPropertyType()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
        LoadPropertyType()
    End Sub

#End Region

    Private Sub LoadPropertyType()

        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        objDB.OpenConnection()
        If txtSearch.Text = "" Then
            objDB.dbVariable.SQLString = "SELECT TypeId,Type,TypeDesc FROM AW_PropertyType WHERE StatusDelete=0" & lblHidSortType.Text
            objDB.FillData(TempDT, "AW_PropertyType", Database.SQLCommandType.NormalString)
        Else
            objDB.dbVariable.SQLString = "SELECT TypeId,Type,TypeDesc FROM AW_PropertyType WHERE StatusDelete=0 AND Type LIKE @Type" & lblHidSortType.Text
            objDB.AddParameter("Type", Data.SqlDbType.NVarChar, "%" & txtSearch.Text & "%")
            objDB.FillData(TempDT, "AW_PropertyType", Database.SQLCommandType.NormalString)
        End If
        objDB.CloseConnection()

        'Creating a Paged Datasouce
        Dim objPaged As New PagedDataSource
        objPaged.DataSource = TempDT.DefaultView
        objPaged.AllowPaging = True
        objPaged.PageSize = PageSize
        objPaged.CurrentPageIndex = CurrentPage

        lblHeadNoOfItem.Text = TempDT.Rows.Count
        lblFootNoOfItem.Text = TempDT.Rows.Count

        repPropertyType.DataSource = objPaged
        repPropertyType.DataBind()

        lblMaxPageCount.Text = objPaged.PageCount - 1
        lblTotalPage.Text = " of " & objPaged.PageCount
    End Sub

    Protected Sub imgNewPropertyType_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgNewPropertyType.Click
        txtPropertyType.Text = String.Empty
        txtPropertyTypeDesc.Text = String.Empty
        mpeEntry.Show()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")
        ElseIf Session.Item("StaffType").ToString = "2" Then
            Response.Redirect("Default.aspx")
        Else
            lblStaffUID.Text = Session("StaffId").ToString
        End If

        If Not Page.IsPostBack Then
            LoadPropertyType()
        End If
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadPropertyType()
    End Sub

    Protected Sub repPropertyType_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repPropertyType.ItemCommand

        If e.CommandName.ToUpper = "EDITITEM" Then
            txtPropertyType.Text = CType(repPropertyType.Items(e.CommandArgument).FindControl("lblRepPropertyType"), Label).Text
            txtPropertyTypeDesc.Text = CType(repPropertyType.Items(e.CommandArgument).FindControl("lblRepProperTypeDesc"), Label).Text
            lblPropertyTypeUID.Text = CType(repPropertyType.Items(e.CommandArgument).FindControl("lblRepPropertyTypeUID"), Label).Text
            mpeEntry.Show()
        Else
            'Begin Delete
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))

            objDB.OpenConnection()
            objDB.dbVariable.SQLString = "UPDATE AW_PropertyType SET StatusDelete=1 WHERE TypeId=@TypeId"
            objDB.AddParameter("TypeId", Data.SqlDbType.UniqueIdentifier, New Guid(CType(repPropertyType.Items(e.CommandArgument).FindControl("lblRepPropertyTypeUID"), Label).Text))
            objDB.ExecuteQuery()
            objDB.CloseConnection()
            lblMsgSuccessful.Text = "Property Type Deleted"
            lblMsgSuccessful.Visible = True
            LoadPropertyType()
        End If
    End Sub

    Private Sub ClearAllField()
        txtPropertyType.Text = String.Empty
        txtPropertyTypeDesc.Text = String.Empty
        lblPropertyTypeUID.Text = String.Empty
        divError.Visible = False
        divSuccessful.Visible = False
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If lblPropertyTypeUID.Text = "" Then
            'New
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))

            objDB.OpenConnection()
            objDB.dbVariable.SQLString = "INSERT INTO AW_PropertyType (TypeId,Type,TypeDesc,CreateDate,StatusDelete) VALUES (@TypeId,@Type,@TypeDesc,@CreateDate,@StatusDelete)"
            objDB.AddParameter("TypeId", Data.SqlDbType.UniqueIdentifier, Guid.NewGuid)
            objDB.AddParameter("Type", Data.SqlDbType.NVarChar, txtPropertyType.Text.Trim.ToUpper)
            objDB.AddParameter("TypeDesc", Data.SqlDbType.NVarChar, txtPropertyTypeDesc.Text.Trim.ToUpper)
            objDB.AddParameter("CreateDate", Data.SqlDbType.DateTime, Now)
            objDB.AddParameter("StatusDelete", Data.SqlDbType.TinyInt, 0)
            objDB.ExecuteQuery()
            objDB.CloseConnection()
            ClearAllField()
            txtSearch.Text = String.Empty
            divSuccessful.Visible = True
            lblMsgSuccessful.Text = "Property Type Successfully Inserted"
            LoadPropertyType()
        Else
            'Edit
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))

            objDB.OpenConnection()
            objDB.dbVariable.SQLString = "UPDATE AW_PropertyType SET Type=@Type,TypeDesc=@TypeDesc WHERE TypeId=@TypeId"
            objDB.AddParameter("TypeId", Data.SqlDbType.UniqueIdentifier, New Guid(lblPropertyTypeUID.Text))
            objDB.AddParameter("Type", Data.SqlDbType.NVarChar, txtPropertyType.Text.Trim.ToUpper)
            objDB.AddParameter("TypeDesc", Data.SqlDbType.NVarChar, txtPropertyTypeDesc.Text.Trim.ToUpper)
            objDB.ExecuteQuery()
            objDB.CloseConnection()
            ClearAllField()
            divSuccessful.Visible = True
            lblMsgSuccessful.Text = "Property Type Successfully Updated"
            LoadPropertyType()
        End If
    End Sub

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function

    Protected Sub lnkType_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkType.Click
        If lnkType.Text = "Property Type▼" Then
            lnkType.Text = "Property Type▲"
        Else
            lnkType.Text = "Property Type▼"
        End If

        lblHidSortType.Text = " ORDER BY Type " & SortType(lnkType.Text) & ", TypeDesc " & SortType(lnkTypeDesc.Text) 
        LoadPropertyType()
    End Sub

    Protected Sub lnkTypeDesc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkTypeDesc.Click
        If lnkTypeDesc.Text = "Property Type Description▼" Then
            lnkTypeDesc.Text = "Property Type Description▲"
        Else
            lnkTypeDesc.Text = "Property Type Description▼"
        End If

        lblHidSortType.Text = " ORDER BY TypeDesc " & SortType(lnkTypeDesc.Text) & ", Type " & SortType(lnkType.Text)
        LoadPropertyType()
    End Sub

    Protected Sub repPropertyType_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repPropertyType.ItemDataBound
        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
            CType(e.Item.FindControl("cbeDelete"), Object).confirmText = "Are you sure you want to delete " & CType(e.Item.FindControl("lblRepPropertyType"), Label).Text
        End If
    End Sub
End Class
